# -*- coding: utf-8 -*-
"""
Created on Sun Oct  3 10:12:11 2021

@author: 刘长奇-2019300677
"""

import sklearn.linear_model as lm
import matplotlib.pyplot as plt 
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
import numpy as np
# load data
digits = load_digits()

# copied from notebook 02_sklearn_data.ipynb
fig = plt.figure(figsize=(6, 6))  # figure size in inches
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
j=0
pca = PCA(n_components=2, svd_solver="randomized")
proj = pca.fit_transform(digits.data)
model = lm.LogisticRegression(solver="newton-cg", C=100, multi_class="auto")
model.fit(proj,digits.target)
result = model.predict(proj)
for i in range(np.shape(result)[0]):
    if result[i]==digits.target[i]:
        j=j+1
print('准确率为：',j/np.shape(result)[0])
plt.subplot(1,2,1)
plt.title('the initial data')
plt.scatter(proj[:, 0], proj[:, 1], c=digits.target,marker='*')
plt.colorbar()
plt.subplot(1,2,2)
plt.title('sklearn-logistic')
plt.scatter(proj[:, 0], proj[:, 1],c=result,marker='*')
plt.colorbar()
plt.show()